package top.continew.admin.webapi.jobfair;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import top.continew.admin.common.model.dto.LoginUser;
import top.continew.admin.common.util.helper.LoginHelper;
import top.continew.starter.extension.crud.enums.Api;

import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.web.bind.annotation.*;

import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
import top.continew.starter.extension.crud.controller.BaseController;
import top.continew.admin.jobfair.model.query.JobPositionResumeQuery;
import top.continew.admin.jobfair.model.req.JobPositionResumeReq;
import top.continew.admin.jobfair.model.resp.JobPositionResumeDetailResp;
import top.continew.admin.jobfair.model.resp.JobPositionResumeResp;
import top.continew.admin.jobfair.service.JobPositionResumeService;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.extension.crud.util.ValidateGroup;
import top.continew.starter.web.model.R;

/**
 * 岗位简历（投递）管理 API
 *
 * @author xiahaike
 * @since 2024/09/14 08:45
 */
@Tag(name = "岗位简历（投递）管理 API")
@RestController
@CrudRequestMapping(value = "/jobfair/jobPositionResume", api = {Api.PAGE, Api.GET, Api.ADD, Api.UPDATE, Api.DELETE, Api.EXPORT})
public class JobPositionResumeController extends BaseController<JobPositionResumeService, JobPositionResumeResp, JobPositionResumeDetailResp, JobPositionResumeQuery, JobPositionResumeReq> {

    @Resource
    private JobPositionResumeService jobPositionResumeService;


    @Operation(
            summary = "分页查询列表",
            description = "分页查询列表"
    )
    @ResponseBody
    @GetMapping
    public R<PageResp<JobPositionResumeResp>> page(JobPositionResumeQuery query, @Validated PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        Long id = loginUser.getId();
        // 根据公司id 查position表得到所有的positionId,再根据position_resumeId查resume_id和position_id
        query.setUserId(id);
        return R.ok(jobPositionResumeService.myPage(query, pageQuery));
    }

    @Override
    @ResponseBody
    @PostMapping
    public R<Long> add(@RequestBody JobPositionResumeReq req) {
        this.checkPermission(Api.ADD);
        // 查询是否有重复数据
        if (this.baseService.checkDuplicate(req)) {
            return R.fail("已投递过该岗位，请勿重复投递");
        }
        return R.ok("新增成功", this.baseService.add(req));
    }
}